<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
// use App\Http\Requests\GoodsDoaddRequest;  //引入分类输入验证的文件 ,注意命名空间
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    // 商品列表
    public function getIndex(Request $request)
    {
      //获取商品类别
      $cates = $this->getCates();

      // 判断是否有查询条件 
      if(!empty($request->input('cid')) && !empty($request->input('keywords'))){
        $goods = DB::table('goods')->leftJoin('cates', 'goods.cid', '=', 'cates.cid')->leftJoin('shops', 'goods.sid', '=', 'shops.sid')->where('goods.gname','like','%'.$request->input('keywords').'%')->where('goods.cid','=',$request->cid)->paginate(15);
      }else if(empty($request->input('cid')) && !empty($request->input('keywords'))){
        $goods = DB::table('goods')->leftJoin('cates', 'goods.cid', '=', 'cates.cid')->leftJoin('shops', 'goods.sid', '=', 'shops.sid')->where('goods.gname','like','%'.$request->input('keywords').'%')->paginate(15);
      }else if(!empty($request->input('cid')) && empty($request->input('keywords'))){
        $goods = DB::table('goods')->leftJoin('cates', 'goods.cid', '=', 'cates.cid')->leftJoin('shops', 'goods.sid', '=', 'shops.sid')->where('goods.cid','=',$request->cid)->paginate(15);
      }else{
        $goods = DB::table('goods')->leftJoin('cates', 'goods.cid', '=', 'cates.cid')->leftJoin('shops', 'goods.sid', '=', 'shops.sid')->paginate(15);  // 分页
      }

      $list = $request->all();  //获取条件参数,用于下一页时传参

      return view('admin.goods.index',['cates'=>$cates,'goods'=>$goods,'list'=>$list]);  //解析模板 分配数据
    }

    // 用于查询商品详情信息等(ajax)
    public function getDetailcheck()
    {
      $gid = $_GET['gid']; //获取商品gid

      $gcomments = DB::select('select gcomments.*,users.username from gcomments left join users on gcomments.gcuid = users.uid where gcomments.gid = '.$gid.' order by gcomments.gctime desc');
      
      echo json_encode($gcomments); 
    }

    // 添加商品
    public function getAdd()
    {
    	//获取商品类别
    	$cates = $this->getCates();

      // 获取所有商店
      $shops = DB::select('select * from shops');

    	//解析添加商品模版
    	return view('admin.goods.add',['cates'=>$cates,'shops'=>$shops]);
    }

    // 处理商品添加时的表单验证(ajax)
    public function getAddcheck()
    {

      $sid = $_GET['sid'];
      $gname = $_GET['gname'];

      // 判断商品名是否重复
      $res = DB::select("select * from goods where sid = ".$sid." and gname = "."'".$gname."'");
      if($res){
        echo 1;
      }
    }

    // 检查是否有子分类(ajax)
    public function getCatecheck()
    {
      $cid = $_GET['cid'];

      $res = DB::select("select * from cates where pid = ".$cid);

      if($res){
        echo 1;
      }
    }

    // 处理商品添加
    public function postDoadd(Request $request)
    {
      // dd($request->all());
      $data = $request->except('_token');
      
      $data['gctime'] = date('Y-m-d H:i:s',time());  // 添加时间
      
      $data['gpic'] = $this->upload($request,'gpic'); // 处理图片
      // dd($data);
      $res = DB::table('goods')->insert($data);
      if($res){
        return redirect('/admin/goods/index')->with('success','添加分类成功');
      }else{
        return back()->with('error','添加分类失败');
      }
    }

    // 封装一个方法用来处理分类图片上传
    private function upload($request,$file)
    {
        if($request->hasFile($file)){
            $suffix = $request->file($file)->getClientOriginalExtension(); // 获取文件后缀名
            $name = md5(time()).rand(0,99999);  //生成一个随机文件名
            $request->file($file)->move('./uploads/goodsimg/',$name.'.'.$suffix);  //移动
            return '/uploads/goodsimg/'.$name.'.'.$suffix;  //返回路径信息
        }
    }

    //获取商品类别
    public function getCates()
    {
      //查询分类数据
      // $cates = DB::table('cates')->get();
      $cates = DB::select("select *,concat(path,',',cid) as paths from cates order by paths");
      foreach ($cates as $k => $v) {
        $arr = explode(',',$v->path);
        $len = count($arr)-1;
        $cates[$k]->cname = str_repeat('|--->',$len).$v->cname;
   		}

   		return $cates;
   	}	

    // 删除
    public function postDel(Request $request)
    {
        // 执行删除操作
        $res = DB::table('goods')->where('gid',$request->input('gid'))->delete();  //删除用户
        if($res){
            echo 1; //返回  ajax判断
        }
    }

    // 修改商品信息
    public function getEdit(Request $request)
    {
        //获取商品类别
        $cates = $this->getCates();
        // dd($cates);
        // 获取所有商店
        $shops = DB::select('select * from shops');
        // dd($shops);
        // 查询需要修改的分类信息
        $gid = $request->gid;  //获取gid
        $goods = DB::table('goods')->where('gid',$gid)->first(); 
        return view('admin.goods.edit',['goods'=>$goods,'cates'=>$cates,'shops'=>$shops]);
    }

    // 修改商品信息时的表单验证
    public function getEditcheck()
    {
      $gname = $_GET['gname'];  //获取新的gname
      $sid = $_GET['sid'];  //获取商店名
      $gid = $_GET['gid'];  //获取商品gid

      // 查原gname
      $oldgname = DB::table('goods')->select('gname')->where('sid',$sid)->where('gid',$gid)->get()[0]->gname;
      if($gname == $oldgname){
        echo 2;die;
      }

      $res = DB::table('goods')->where('sid',$sid)->where('gname',$gname)->get();
      if($res){
        echo 1;
      }
    }

    // 处理修改商品信息
    public function postDoedit(Request $request)
    {   
        $data = $request->except('_token','oldgpic');

        // 判断是否修改图片
        if(empty($request->gpic)){
            $data['gpic'] = $request->oldgpic;
        }else{
            $data['gpic'] = $this->upload($request,'gpic');  //调用函数处理图片
        }

        $res = DB::table('goods')->where('gid',$request->gid)->update($data);

        if($res){
            return redirect('/admin/goods/index')->with('success','修改商品成功');
        }else{
            return back()->with('error','修改商品失败');
        }
    }

}
